<template>
<div>
<template>


  <!-- 页面主体 -->
   <el-row :gutter="0">

    <!-- 输入框 -->
    <el-col :span="6">
        <el-input
        v-model="dormitoryDTO.username"
        class="w-50 m-2"
        size="small"
        placeholder="请输入姓名"
        @input="search()"
        />
    </el-col>

    <!-- 搜索按钮 -->
    <el-col :span="3">
      <el-button size="small"  type="primary" @click="search()">搜索</el-button>
    </el-col>

  </el-row>

  <!-- 表格 -->
  <el-table :data="dormitorys" stripe border height="300" id="mytable" >
    <el-table-column prop="id" sortable=true label="ID" width="60"/>
    <el-table-column prop="buildingNo" sortable=true label="栋号"  width="120"/>
    <el-table-column prop="roomNo" sortable=true label="房间号"  width="120"/>
    <el-table-column prop="dermitoryHeadName" sortable=true label="宿舍长"  width="120" />
        <el-table-column label="操作">
            <template #default="scope">
                <el-button size="small" @click="lookSecurityQuestion(scope.row)" v-if="role === '宿舍长登录'">
                   查看安全问题
                </el-button>
                <el-button size="small" type="danger" @click="lookScore(scope.row.id)" v-if="role === '宿舍长登录'">
                   查看打分
                </el-button>
                <el-button size="small" @click="securityQuestion(scope.row)" v-if="role != '宿舍长登录' && role != '学生会登录'">
                   安全检查
                </el-button>
                <el-button size="small" type="danger" @click="score(scope.row.id)" v-if="role != '宿舍长登录'">
                   打分
                </el-button>
            </template>
            </el-table-column>
  </el-table>

  <!-- 分页部分 -->
  <el-pagination
    small
    background
    layout="prev, pager, next"
    :page-size="dormitoryDTO.capacity"
    :current-page.sync="dormitoryDTO.index"
    @current-change="changeCurrent"
    :total="count"
    class="mt-4"
  />
</template>

</div>
</template>
<script>
export default {
  name: 'Home',
  data () {
    return {
      // 当前登录的用户角色
      role: '宿舍',
      // 表格映射对象
      dormitorys: [],
      // 修改的对象
      uDormitory: {
        username: '',
        password: '',
        roleId: '',
        dormitoryNo: '',
        dermitoryHeadId: ''
      },
      // 表格查询对象
      dormitoryDTO: {
          index: 0,
          capacity: 10,
          username: ''
      },
      // 角色列表
      roles: [],
      dormitoryHeads: [],
      // 添加宿舍弹出控制
      dialogFormVisible: false,
      // 修改宿舍弹出控制
      updateDialogFormVisible: false,
      // 添加宿舍实体
      dormitory: {
          username: '',
          password: '',
          roleId: '',
          dormitoryNo: ''
      },
      // 宿舍列表总数
      count: 0,
      // 用户信息
      user: {},
      // 角色信息
      role: ''
    }
  },
  // 页面初始化函数 
  created () {

    // 获得角色信息
    this.role = window.sessionStorage.getItem('role')

    // 获得用户信息
    this.user = JSON.parse(window.sessionStorage.getItem('user'))

    this.$http({
        url: "/dormitory",
        params: {
            index: this.dormitoryDTO.index,
            capacity: this.dormitoryDTO.capacity,
            username: this.dormitoryDTO.username
        }
    }).then(response => {
       if (this.role === '宿舍长登录') {
          for(var i=0;i<response.data.length;i++) {
              if (response.data[i].dermitoryHeadName === this.user.username) {
                  this.dormitorys.push(response.data[i])
              }
          }

       } else {
            this.dormitorys = response.data
       } 
    })
    this.$http({
        url: "/dormitoryHead",
        params: {
            index: 1,
            capacity: 1,
            username: ''
        }
    }).then(response => {
       this.dormitoryHeads = response.data
       console.log(this.dormitoryHeads)
    })

  },
  methods: {

    // 重任宿舍长
    reset(data) {
        this.uDormitory = data
        this.updateDialogFormVisible = true
    },

    // 改变当前页
    changeCurrent() {
      findAll()
    },
    // 添加宿舍
     adddormitory() {
       this.dialogFormVisible=true;
     },

     // 查看宿舍安全问题
     lookSecurityQuestion(data) {
       window.sessionStorage.setItem('d_id', data.id)
       this.$router.push('/home/security/user')
     },

     securityQuestion(data) {
       window.sessionStorage.setItem('d_id', data.id)
       this.$router.push('/home/osecurity/user')
     },

     // 查看宿舍打分
     lookScore(id) {
       window.sessionStorage.setItem('d_id', id)
       this.$router.push('/home/score/user')
     },

          // 查看宿舍打分
     score(id) {
       window.sessionStorage.setItem('d_id', id)
       this.$router.push('/home/oscore/user')
     },

     
     ensureAdddormitory() {
      this.$http.post("/dormitory", this.dormitory).then(response => {
        this.$message({
          message: '添加宿舍成功',
          type: 'success'
        });
        this.cleardormitory()
        this.findAll()
      })
    },
    // 取消添加宿舍
    cancelAdddormitory() {
      this.cleardormitory()
    },
    ensureUpdatedormitory() {
      this.$http.put('/dormitory', this.uDormitory).then(response => {
        this.$message({
          message: '重任宿舍长成功',
          type: 'success'
        });   
        this.updateDialogFormVisible=false
        this.findAll()
      })
    },
    cancelUpdateddormitory() {
      this.updateDialogFormVisible=false
    },
    // 搜索对象，并映射
    search() {
      this.findAll()
    },
    // 查询所有，并映射
    findAll() {
    this.$http({
        url: "/dormitory",
        params: {
            index: this.dormitoryDTO.index,
            capacity: this.dormitoryDTO.capacity,
            username: this.dormitoryDTO.username
        }
    }).then(response => {
       this.dormitorys = response.data
       console.log(this.dormitorys)
    })
    },
    // 清除宿舍，并关闭窗口
    cleardormitory() {
        this.dormitory.username=''
        this.dormitory.password=''
        this.dormitory.roleId=''
        this.dormitory.dormitoryNo=''
        this.dialogFormVisible=false
     },


    // 导出Excel
    exportExcel () {
        var xlsxParam = { raw: true } 
        var wb = this.$xlsx.utils.table_to_book(document.querySelector('#mytable'), xlsxParam)
        /* get binary string as output */
        var wbout = this.$xlsx.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
        try {
            this.$filesaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), this.user.username + '导出宿舍信息表.xlsx')
        } catch (e) {
        if (typeof console !== 'undefined') {
            console.log(e, wbout)
        }
        }
        return wbout
    }

  }
}
</script>
<style>

</style>